package com.hhx.ec.controller;

import javax.annotation.Resource;

import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.client.RestTemplate;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;


@Controller
public class RibbonControllerTest {
	
	@Resource
	private  RestTemplate restTemplate;
	@Resource
	private LoadBalancerClient LoadBalancerClient;
	
	
	@RequestMapping(value = "/tx" ,method = {RequestMethod.GET})
	@ResponseBody
	@HystrixCommand(fallbackMethod = "errorQuery")
	public Object  ribbonTest() {
		return restTemplate.getForObject("http://eureka-client-1/test_ribbon/eutext?name=huangyun", String.class);
	}
	/***
	 *  测试 熔断器作用
	 * @return
	 */
	public  String errorQuery() {
		return  "请求终止！！！";
		
	}
	
	
	@RequestMapping(value = "/getRibbonInfo" ,method = {RequestMethod.GET})
	@ResponseBody
	public Object  getRibbonInfo() {
		
		ServiceInstance choose = LoadBalancerClient.choose("eureka-client-1");
		
		return choose.getHost()+":"+choose.getPort();
	}
	
	
	
	
}
